﻿@namespace Masa.Blazor
@inherits MasaComponentBase
@typeparam TItem
@typeparam TItemValue
@typeparam TValue

<MList Class="m-select-list"
       Dense="Dense"
       role="listbox"
       tabindex="-1">
    @{
        var computedItems = HideSelected ? Items.Where(u => !HasItem(u)).ToList() : Items;

        @PrependItemContent
        if (computedItems.Count > 0)
        {
            @foreach(var item in computedItems)
            {
                @VirtualizeItemContent(item)
            }
        }
        else
        {
            @(NoDataContent ?? DefaultNoDataContent())
        }

        @AppendItemContent
    }
</MList>

@code {

    private RenderFragment DefaultNoDataContent() => __builder =>
    {
        <MListItem @onmousedown:preventDefault
                   role="undefined"
                   Title="@NoDataText">
        </MListItem>
    };

    private RenderFragment VirtualizeItemContent(TItem item) => __builder =>
    {
        var itemHeader = ItemHeader?.Invoke(item);
        if (itemHeader != null)
        {
            <MSubheader>@itemHeader</MSubheader>
            return;
        }

        var itemDivider = ItemDivider?.Invoke(item);
        if (itemDivider is true)
        {
            <MDivider/>
            return;
        }

        var computedItems = HideSelected ? Items.Where(u => !SelectedItems.Any(v => v.Item?.Equals(u) is true)).ToList() : Items;
        var isActive = HasItem(item);
        var isDisabled = GetDisabled(item);
        var highlight = computedItems.IndexOf(item) == SelectedIndex;

        <MListItem @onmousedown:preventDefault
                   @key="@item"
                   Disabled="@isDisabled"
                   ActiveClass="@TileActiveClass"
                   Ripple
                   IsActive="@isActive"
                   Highlighted="@highlight"
                   OnClick="@OnClick"
                   role="option"
                   aria-selected="@isActive.ToString()">
            @if (ItemContent == null)
            {
                if (Action && !HideSelected && Items.Count > 0)
                {
                    <MListItemAction>
                        <MSimpleCheckbox Value="@isActive"
                                         Color="@Color"
                                         Ripple="@false"
                                         ValueChanged="@OnClick">
                        </MSimpleCheckbox>
                    </MListItemAction>
                }

                <MListItemContent>
                    <MListItemTitle>
                        @((MarkupString)GetFilteredText(item))
                    </MListItemTitle>
                </MListItemContent>
            }
            else
            {
                @ItemContent(new SelectListItemProps<TItem>(item, OnClick))
            }
        </MListItem>

        async Task OnClick()
        {
            if (isDisabled)
            {
                return;
            }

            await OnSelect.InvokeAsync(item);
        }
    };
}